<?php
include_once "BaseMapper.php";
/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
class Model_Order_DetailBase{
    protected $_id;
    protected $_product_id;
    protected $_quantity;
    protected $_price;
    protected $_factor;
    protected $_order_id;
    protected $_charges;
    protected $_agent_id;
    protected $_order_status_id;
    protected $_order_created;
    protected $_modified;
    protected $_modified_by;


    public function __construct(array $options = NULL)   
    {   
        if (is_array($options)) {   
            $this->setOptions($options);   
        }   
    }   
    public function __set($name, $value)   
    {   
        $method = "set" . $name;   
        if (("mapper" == $name) || !method_exists($this, $method)) {   
            throw new Exception("Invalid Order_Status property");   
        }   
        $this->$method($value);   
    }   
    public function __get($name)
    {
        $method = "get" . $name;
        if (("mapper" == $name) || !method_exists($this, $method)) {
            throw new Exception("Invalid Order_Status property");
        }
        return $this->$method();
    }
    public function setOptions(array $options)
    {
        $methods = get_class_methods($this);
        foreach ($options as $key => $value) {
            $method = "set" . ucfirst($key);
            if (in_array($method, $methods)) {
                $this->$method($value);
            }
        }
        return $this;
    }
    
    public function setId($value){
        $this->_id=$value;
        return $this;
    }
    public function getId(){return $this->_id;}
    
    public function setProduct_Id($value){
        $this->_product_id=$value;
        return $this;
    }
    public function getProduct_Id(){return $this->_product_id;}
    
    public function setPrice($value){
        $this->_price=$value;
        return $this;
    }
    public function getPrice(){return $this->_price;}
    
    public function setCharges($value){
        $this->_charges=$value;
        return $this;
    }
    public function getCharges(){return $this->_charges;}
    
    public function setOrder_Id($value){
        $this->_order_id=$value;
        return $this;
    }
    public function getOrder_Id(){return $this->_order_id;}
    
    public function setQuantity($value){
        $this->_quantity=$value;
        return $this;
    }
    public function getQuantity(){return $this->_quantity;}
    
    public function setFactor($value){
        $this->_factor=$value;
        return $this;
    }
    public function getFactor(){return $this->_factor;}
    
    public function setAgent_Id($value){
        $this->_agent_id=$value;
        return $this;
    }
    public function getAgent_Id(){return $this->_agent_id;}
    
    public function setOrder_Status_Id($value){
        $this->_order_status_id=$value;
        return $this;
    }
    public function getOrder_Status_Id(){return $this->_order_status_id;}
    
    public function setOrder_No($value){
        $this->_order_no=$value;
        return $this;
    }
    public function getOrder_No(){return $this->_order_no;}
    
    public function setOrder_Created($value){
        $this->_order_created=$value;
        return $this;
    }
    public function getOrder_Created(){return $this->_order_created;}
    public function setModified($value){
        $this->_modified=$value;
        return $this;
    }
    public function getModified(){return $this->_modified;}
    public function setModified_By($value){
        $this->_modified_by=$value;
        return $this;
    }
    public function getModified_By(){return $this->_modified_by;}
}
class Model_DbTable_Order_Detail extends Zend_Db_Table_Abstract{
    protected $_name="order_detail";
    protected $_primary="id";
}
class Model_Order_DetailMapperBase extends BaseMapper{
    protected $_dbTable;
     public function setDbTable($dbTable)
    {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception("Invalid table data gateway provided");
        }
        $this->_dbTable = $dbTable;
        return $this;
    }
 
    public function getDbTable()
    {
        if (NULL === $this->_dbTable) {
            $this->setDbTable("Model_DbTable_Order_Detail");
        }
        return $this->_dbTable;
    }
     public function save(Model_Order_Detail $entry){
        $data = array(
                'id' => $entry->getId(),
                'agent_id'=> $entry->getAgent_Id(), 
                'charges'=>$entry->getCharges(),
                'factor'=>$entry->getFactor(),
                'order_id'=>$entry->getOrder_Id(),
                'price'=>$entry->getPrice(),
                'quantity'=>$entry->getQuantity(),
                'product_id'=>$entry->getProduct_Id(),
                'order_status_id'=>$entry->getOrder_Status_Id(),
                'order_created'=>$entry->getOrder_Created(),
                'modified'=>$entry->getModified(),    
                'modified_by'=>$entry->getModified_By(),
                );
         if (null === ($id = $entry->getId())) {
            unset($data["id"]);
            $this->getDbTable()->insert($data);
        } else {
            $this->getDbTable()->update($data, array("id = ?" => $id));
        }
    }
     public function delete($id){
        $this->getDbTable()->delete("id=".$id);
    }
    public function find($id, Model_Order_Detail $entry){
        $result = $this->getDbTable()->find($id);
        if(0== count($result))
        {
            return;
        }
        $row=$result->current();
        $entry->setId($row->id)
                ->setId($row->id)
                ->setAgent_Id($row->agent_id)
                ->setCharges($row->charges)
                ->setFactor($row->factor)
                ->setOrder_Id($row->order_id)
                ->setPrice($row->price)
                ->setQuantity($row->quantity)
                ->setProduct_Id($row->product_id)
                ->setOrder_Status_Id($row->order_status_id)
                ->setOrder_Created($row->order_created)
                ->setModified($row->modified)
                ->setModified_By($row->modified_by);
    }
     public function fetchAll(){
        $result = $this->getDbTable()->fetchAll();
        $entries = array();
        foreach ($result as $row){
            $entry = new Model_Order_Detail();
            $entry->setId($row->id)
                  ->setId($row->id)
                ->setAgent_Id($row->agent_id)
                ->setCharges($row->charges)
                ->setFactor($row->factor)
                ->setOrder_Id($row->order_id)
                ->setPrice($row->price)
                ->setQuantity($row->quantity)
                ->setProduct_Id($row->product_id)
                ->setOrder_Status_Id($row->order_status_id)
                ->setOrder_Created($row->order_created)
                ->setModified($row->modified)
                ->setModified_By($row->modified_by);
            $entries[]=$entry;
        }
        return $entries;
    }
    public function fetchAllByOrderNo($order_no){
        $db = Zend_Db_Table::getDefaultAdapter();
        $select=$db->select()
                ->from('order_detail')
                ->where('order_id = '.$order_no)
                ;
        $stmt=$select->query();
        $result = $stmt->fetchAll(PDO::FETCH_CLASS);
        $stmt->closeCursor();
            
        //$result = $this->getDbTable()->fetchAll();
        $entries = array();
        foreach ($result as $row){
            $entry = new Model_Order_Detail();
            $entry->setId($row->id)
                  ->setId($row->id)
                ->setAgent_Id($row->agent_id)
                ->setCharges($row->charges)
                ->setFactor($row->factor)
                ->setOrder_Id($row->order_id)
                ->setPrice($row->price)
                ->setQuantity($row->quantity)
                ->setProduct_Id($row->product_id)
                ->setOrder_Status_Id($row->order_status_id)
                ->setOrder_Created($row->order_created)
                ->setModified($row->modified)
                ->setModified_By($row->modified_by);
            $entries[]=$entry;
        }
        return $entries;
    }
    public function fetchAllArray()
    {
		$data = array(array());
        $resultSet = $this->getDbTable()->fetchAll(PDO::FETCH_COLUMN);
		$count = count($resultSet);
		for($i=0;$i<$count;$i++)
		{
			$data[$resultSet[$i]['id']]['name'] = $resultSet[$i]['name'];
		}
		return $data;
    }
    public function fetchPaging($page, $pageSize, $orderBy, $orderDirection, &$totalRecord, $filters=null)
    {
        $db = Zend_Db_Table::getDefaultAdapter();	
		$aFilterValues = array();
		$sFilter = $this->createWhereCondition($filters, $aFilterValues);
		$queryTotal = "SELECT count(*) as totals FROM order_detail ".$sFilter."";
		$totalRecord = $this->getTotalRecords($queryTotal, $aFilterValues);
		$query = "SELECT * FROM order_detail ".$sFilter." order by ".$orderBy." ".$orderDirection." limit ".($page-1)*$pageSize.",".$pageSize."";
        $stmt = $db->query($query, $aFilterValues);		
        $rows = $stmt->fetchAll(PDO::FETCH_CLASS);
        $stmt->closeCursor();
        $entries   = array();
        foreach ($rows as $row) {
            $entry = new Model_Order_Detail();
            $entry->setId($row->id)
                  ->setId($row->id)
                ->setAgent_Id($row->agent_id)
                ->setCharges($row->charges)
                ->setFactor($row->factor)
                ->setOrder_Id($row->order_id)
                ->setPrice($row->price)
                ->setQuantity($row->quantity)
                ->setProduct_Id($row->product_id)
                ->setOrder_Status_Id($row->order_status_id)
                ->setOrder_Created($row->order_created)
                ->setModified($row->modified)
                ->setModified_By($row->modified_by);
            $entries[] = $entry;
        }
        return $entries;
    }
    
}